//
//  chatPage.swift
//  skincare
//
//  Created by yang on 2023/5/9.
//

import SwiftUI
struct ChatBubble: View {
    var message: String
    var isMe: Bool

    var body: some View {
        HStack {
            if isMe {
                Spacer()
            }
            Text(message)
                .foregroundColor(.primary)
                .padding()
                .background(isMe ? Color.green : Color.gray)
                .cornerRadius(12)
            if !isMe {
                Spacer()
            }
        }
        .background(Color.clear)
    }
}
extension ChatBubble: Hashable {
    static func == (lhs: ChatBubble, rhs: ChatBubble) -> Bool {
        return lhs.message == rhs.message && lhs.isMe == rhs.isMe
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(message)
        hasher.combine(isMe)
    }
}

struct ChatView: View {
    @State private var newMessage = ""
    @State private var chatList:[ChatBubble] = []
    
    var body: some View {
        GeometryReader { geometry in
            VStack {
                List {
                    ForEach(chatList, id: \.self) { chatBubble in chatBubble }
                }
                        
                ZStack(alignment: .bottomTrailing) {
                    HStack {
                        TextEditor(text: $newMessage)
                            .frame(minWidth: 0, maxWidth: geometry.size.width/1.3, minHeight: 0, maxHeight: geometry.size.height/3)
                            .padding(.all, 8.0)
                            .background(Color.white)
                            .cornerRadius(10)
                            .shadow(radius: 2)
                    }
                    .padding(.trailing, 15.0)
        
                    Button(action: sendMessage) {
                        HStack {
                            Image(systemName: "paperplane.fill")
                                .foregroundColor(.white)
                            Text("发送")
                                .foregroundColor(.white)
                        }
                        .padding(.horizontal, 16)
                        .padding(.vertical, 8)
                        .background(RoundedRectangle(cornerRadius: 10).fill(Color.green))
                    }
                    .padding(.bottom, 15.0)
                    .padding(.trailing, 32.0)
                }
                .padding(.leading, 20.0)
            }
        }
    }
    
        
    func sendMessage() {
        if newMessage.isEmpty {
            return
        }
        let message = newMessage
        newMessage = ""
        home_provider.request(.getGPT(prompt: message))
        { result in
            switch result {
            case let .success(response):
                do {
                    let responseData = response.data
                    
                // 将JSON对象解码为自定义的数据模型
                    let decodedObject = try JSONDecoder().decode(ResponseData.self, from: responseData)
                        let content = decodedObject.response.message.content
                        print(content)
                    //跑通了但不知道怎么解码
                    let chatBubble = ChatBubble(message: content, isMe: false)
                    chatList.append(chatBubble)
                                
                } catch let error {
                    print(error)
                }
            case let .failure(error):
                print(error)
            }
        }
        let chatBubble = ChatBubble(message: message, isMe: true)
        // 将新的对话气泡添加到聊天记录中
        chatList.append(chatBubble)
    }
}

struct ChatView_Previews: PreviewProvider {
    static var previews: some View {
        ChatView()
    }
}

struct ResponseData: Codable {
    let response: Response
}
struct Response: Codable {
    let finish_reason: String
    let index: Int
    let message: Message
}
struct Message: Codable {
    let content: String
    let role: String
}

